<template>
  <div class="container-randoms">
    <el-card>
      <!-- 关键字 -->
      <el-row>
        <el-col>
          <el-form label-width="80px" inline>
            <el-form-item label="关键字">
              <el-input
                placeholder="根据编号搜索"
                v-model="random.keyword"
              ></el-input>
            </el-form-item>
            <el-form-item style="float: right">
              <el-button size="small" @click="clearn">清除</el-button>
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <!-- 总数据 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="randoms" :header-cell-style="{ background: '#f4f4f5' }">
        <el-table-column label="编号" prop="id" width="200px"></el-table-column>
        <el-table-column label="题型" align="center">
          <template slot-scope="{ row }">
            {{
              row.questionType === "1"
                ? "单选"
                : row.questionType === "2"
                ? "多选"
                : "简答"
            }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="190px" align="center">
          <template slot-scope="{ row }">
            <el-link
              :underline="false"
              type="primary"
              @click="preview(item.id)"
              v-for="(item, index) in row.questionIDs"
              :key="index"
              >{{ item.number }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          prop="addTime"
          width="160px"
          align="center"
        ></el-table-column>
        <el-table-column
          label="答题时间(s)"
          prop="totalSeconds"
          align="center"
        ></el-table-column>
        <el-table-column
          label="正确率(%)"
          prop="accuracyRate"
          align="center"
        ></el-table-column>
        <el-table-column
          label="录入人"
          prop="userName"
          align="center"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              class="
                el-button el-button-danger el-button-small
                elplain
                is-circle
              "
              title="删除"
              plain
              @click="del(row.id)"
            >
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next,sizes,jumper"
        :page-size="random.pagesize"
        :current-page="random.page"
        :total="total"
        :page-sizes="[5, 10, 20]"
        @current-change="changePage"
        @size-change="changeSize"
        class="page"
      >
      </el-pagination>
      <!-- 监听页码尺寸 -->
    </el-card>
    <questionRandom :dialogVisible="dialogVisible" @close="dialogVisible = false" :questionItem="questionItem"></questionRandom>
  </div>
</template>

<script>
// 导入组题列表api
import { randoms, removeRandoms,detail } from "@/api/hmmm/questions";
import questionRandom from '../components/question-random.vue'
export default {
  data() {
    return {
      questionItem: {},
      dialogVisible: false,
      // 组题列表
      randoms: [],
      //组题名字
      random: {
        keyword: null,
        page: 1,
        pagesize: 10,
      },
      total: 0, // 总条数
      
    };
  },
  components: {
    questionRandom
  },
  created() {
    this.getRandoms();
  },
  methods: {
    // 获取组题列表
    async getRandoms() {
      const { data } = await randoms(this.random);
      // console.log(data);
      // 给data赋值
      this.randoms = data.items;
      this.total = data.counts;
    },
    // 分页
    changePage(newPage) {
      this.random.page = newPage;
      this.getRandoms();
    },
    changeSize(newSize) {
      this.random.page = 1;
      this.random.pagesize = newSize;
      this.getRandoms();
    },
    // 搜索
    search() {
      this.random.page = 1;
      this.getRandoms();
    },
    // 清除功能
    clearn() {
      this.random = {
        keyword: null,
        page: 1,
        pagesize: 10,
      };
      this.getRandoms();
    },
    async del(id) {
      try {
        await this.$confirm("确认删除该组题吗");
        // 只有点击了确定 才能进入到下方
        await removeRandoms({ id }); // 调用删除接口
        // 解决当前只有一条数据时，删除之后页面内容为空的bug
        if (this.randoms.length === 1) {
          this.random.page = this.random.page > 1 ? this.random.page - 1 : 1;
        }
        this.getRandoms(); // 重新加载数据
        this.$message.success("删除成功");
      } catch (error) {
        console.log(error);
      }
    },
     // 预览
    async preview (id) {
      try {
        const { data } = await detail({ id })
        console.log(data)
        this.questionItem = data
        this.dialogVisible = true
      } catch (error) {
        this.$message.info('数据未找到')
        return error
      }
    },
  },
};
</script>

<style scoped lang='scss'>
.container-randoms {
  padding: 10px 15px;
  .page {
    margin-top: 15px;
    float: right;
    margin-bottom: 15px;
  }
}
.el-link {
  list-style: none;
}
</style>
